package cn.lblbc.todo

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import java.text.SimpleDateFormat
import java.util.Locale

class TaskGroupAdapter(
    private var taskGroups: List<TaskGroup>,
    private val onTaskCheckedChangeListener: (Task, Boolean) -> Unit,
    private val onTaskClickListener: (Task) -> Unit
) : RecyclerView.Adapter<TaskGroupAdapter.TaskGroupViewHolder>() {

    class TaskGroupViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val dateTextView: TextView = itemView.findViewById(R.id.dateTextView)
        val tasksRecyclerView: RecyclerView = itemView.findViewById(R.id.tasksRecyclerView)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TaskGroupViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_task_group, parent, false)
        return TaskGroupViewHolder(view)
    }

    override fun onBindViewHolder(holder: TaskGroupViewHolder, position: Int) {
        val taskGroup = taskGroups[position]
        holder.dateTextView.text = taskGroup.date
        
        val taskAdapter = TaskItemAdapter(taskGroup.tasks, onTaskCheckedChangeListener, onTaskClickListener)
        holder.tasksRecyclerView.layoutManager = LinearLayoutManager(holder.itemView.context)
        holder.tasksRecyclerView.adapter = taskAdapter
    }

    override fun getItemCount(): Int = taskGroups.size

    fun updateTaskGroups(newTaskGroups: List<TaskGroup>) {
        taskGroups = newTaskGroups
        notifyDataSetChanged()
    }
}